//
//  Menu.m
//  iCan - iPhone Context-Aware Navigation
//  Carnegie Mellon University - 18-842 Distributed Systems
//
//  Created by Andrew Santell on 3/26/12.
//  Copyright (c) 2012 Andrew Santell. All rights reserved.

/* Menu screen displayed to users after they login */
 
 
#import "Menu.h"
#import "iCanAppDelegate.h"
#import "iCanViewController.h"


@interface Menu ()

@end

@implementation Menu
@synthesize appDelegate;
@synthesize picker;
@synthesize result;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    /* Set up user "global variables" */
    appDelegate = [[UIApplication sharedApplication] delegate];

    /* Set Picker size */
    picker.frame = CGRectMake(71, 230, 180, 162);

    /* Sort Picker */
    result = [[appDelegate.nodes allKeys] sortedArrayUsingSelector:@selector(compare:)];
    
    /* Set Default Value for picker */
    NSString *tempString = [result objectAtIndex:0];
    Hosts *host = [appDelegate.nodes objectForKey:tempString];
  
    /* Set global variables for source node */
    appDelegate.nodeName = host.name;
    appDelegate.nodeIP = host.ip;
    NSLog(@"Name = %@, IP = %@", appDelegate.nodeName, appDelegate.nodeIP);
}

- (void)viewDidUnload
{
    [self setPicker:nil];
    [self setResult:nil];
    [self setAppDelegate:nil];
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [appDelegate.nodes count];
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [result objectAtIndex:row];
}

- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSString *tempString = [result objectAtIndex:row];
    Hosts *host = [appDelegate.nodes objectForKey:tempString];
   
    /* Set global variables for source node */
    appDelegate.nodeName = host.name;
    appDelegate.nodeIP = host.ip;
    
    NSLog(@"\nHost name = %@\nHost IP = %@\nHost Port = %lu", appDelegate.nodeName, appDelegate.nodeIP, appDelegate.nodePort);
    

}

@end
